package com.ruoyi.project.cwsk.channelsub.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.project.cwsk.channelsub.mapper.ChannelSubUserMapper;
import com.ruoyi.project.cwsk.channelsub.domain.ChannelSubUser;
import com.ruoyi.project.cwsk.channelsub.service.IChannelSubUserService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 渠道管理Service业务层处理
 * 
 * @author songzc
 * @date 2020-01-03
 */
@Service
public class ChannelSubUserServiceImpl implements IChannelSubUserService 
{
    @Autowired
    private ChannelSubUserMapper channelSubUserMapper;
    
    @Autowired
	private IRoleService roleService;
	
	@Autowired
	private IPostService postService;
	
	@Autowired
	private IUserService userService;
	
	
    /**
     * 查询渠道管理
     * 
     * @param id 渠道管理ID
     * @return 渠道管理
     */
    @Override
    public ChannelSubUser selectChannelSubUserById(Integer id)
    {
        return channelSubUserMapper.selectChannelSubUserById(id);
    }

    /**
     * 查询渠道管理列表
     * 
     * @param channelSubUser 渠道管理
     * @return 渠道管理
     */
    @Override
    public List<ChannelSubUser> selectChannelSubUserList(ChannelSubUser channelSubUser)
    {
        return channelSubUserMapper.selectChannelSubUserList(channelSubUser);
    }

    /**
     * 新增渠道管理
     * 
     * @param channelSubUser 渠道管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertChannelSubUser(ChannelSubUser channelSubUser)
    {
    	// 保存关联的帐号信息
		User user = channelSubUser.getUser();
		if (user != null) {
			user.setUserName(channelSubUser.getChannelName());
			user.setStatus(UserConstants.NORMAL);;
			user.setAcronym(channelSubUser.getChannelAcronym());

			Long[] roleIds = new Long[1];
			Long[] postIds = new Long[1];
			// 默认角色、岗位
			Role role = null;
			Post post = null;
			role = roleService.findByRoleKey("ROLE_CHANNEL_SUBUSER");
			post = postService.selectPostByCode("channel_subuser");
			
			roleIds[0]=role.getRoleId();
			postIds[0]=post.getPostId();
			
			user.setRoleIds(roleIds);
			user.setPostIds(postIds);

			userService.insertUser(user);
		}
		
		channelSubUser.setUserId(user.getUserId());
		channelSubUser.setCreatorId(ShiroUtils.getUserId());
        channelSubUser.setCreateTime(DateUtils.getNowDate());
        
        return channelSubUserMapper.insertChannelSubUser(channelSubUser);
    }

    /**
     * 修改渠道管理
     * 
     * @param channelSubUser 渠道管理
     * @return 结果
     */
    @Override
    public int updateChannelSubUser(ChannelSubUser channelSubUser)
    {
        return channelSubUserMapper.updateChannelSubUser(channelSubUser);
    }

    /**
     * 删除渠道管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteChannelSubUserByIds(String ids)
    {
        return channelSubUserMapper.deleteChannelSubUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除渠道管理信息
     * 
     * @param id 渠道管理ID
     * @return 结果
     */
    @Override
    public int deleteChannelSubUserById(Integer id)
    {
        return channelSubUserMapper.deleteChannelSubUserById(id);
    }

	@Override
	public List<String> findCidsByUserId(Long userId) {
		return channelSubUserMapper.findCidsByUserId(userId);
	}

	@Override
	public ChannelSubUser findSubChannelByUser(Long userId) {
		return channelSubUserMapper.findSubChannelByUser(userId);
	}

	@Override
	public List<ChannelSubUser> findByCreator(Long userId) {
		return channelSubUserMapper.findByCreator(userId);
	}

	@Override
	public List<String> findSubUserCid(Long userId) {
		return channelSubUserMapper.findSubUserCid(userId);
	}
}
